package com.topica.network;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;

import android.app.ProgressDialog;
import android.content.Context;
import android.database.sqlite.SQLiteException;
import android.os.AsyncTask;
import android.widget.Toast;

import com.topica.database.DataBaseHelper;
import com.topica.network.callback.OnGetCourseListener;

public class GetCourseTask extends AsyncTask<String, Void, Integer> {
	private static final String METHOD_INFO_COURSE = "infoCourse";
	private static final String URL = "http://mobi.topica.vn/tinhnv/Service_server/websevice2/client1.php";
	private String course_id;
	private Context context;

	private final int OK = 1;
	private final int FAIL = -1;
	private ProgressDialog dialog;
	private OnGetCourseListener listener;

	public GetCourseTask(Context context, String courseId) {
		this.context = context;
		this.course_id = courseId;
		dialog = new ProgressDialog(context);
	}

	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		dialog.setTitle("Getting data...");
		dialog.show();
	}

	@Override
	protected Integer doInBackground(String... params) {
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
		nameValuePairs
				.add(new BasicNameValuePair("method", METHOD_INFO_COURSE));
		nameValuePairs.add(new BasicNameValuePair("course_id", course_id));

		HttpClient mClient = new DefaultHttpClient();
		HttpParams mParams = new BasicHttpParams();
		mParams.setParameter(CoreProtocolPNames.PROTOCOL_VERSION,
				HttpVersion.HTTP_1_1);
		mClient = new DefaultHttpClient(mParams);
		HttpPost mPost = new HttpPost(URL);
		DataBaseHelper myDbHelper = new DataBaseHelper(context);
		try {
			mPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
			HttpResponse mResponse = mClient.execute(mPost);
			String result = EntityUtils.toString(mResponse.getEntity());
			ArrayList<String> data = CourseParse.parseCourse(result);
			myDbHelper = new DataBaseHelper(context);
			myDbHelper.insertDataToDB(data);
			return OK;
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
			return FAIL;
		} catch (ClientProtocolException e) {
			e.printStackTrace();
			return FAIL;
		} catch (IOException e) {
			e.printStackTrace();
			return FAIL;
		} catch (JSONException e) {
			e.printStackTrace();
			return FAIL;
		} catch (SQLiteException e) {
			e.printStackTrace();
			return FAIL;
		}finally{
			myDbHelper.close();
		}
	}

	@Override
	protected void onPostExecute(Integer result) {
		super.onPostExecute(result);
		dialog.dismiss();
		if (result == OK) {
			listener.onGetCourseListener(course_id);
		} else {
			Toast.makeText(context.getApplicationContext(), "Get Course Fail",
					Toast.LENGTH_SHORT).show();
		}
	}

	public void setOnGetCourseListener(OnGetCourseListener listener) {
		this.listener = listener;
	}
}
